package com.axend.lib_base.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.databinding.BindingAdapter;
import androidx.databinding.InverseBindingAdapter;
import androidx.databinding.InverseBindingListener;

import com.axend.lib_base.R;


public class CustomSettingItemView extends LinearLayout{

    private TextView leftText;   //左边文字
    private TextView rightText;  //右边文字
    private TextView rightTextArrow;  //右边文字
    private Switch aSwitch;  //开关
    private ConstraintLayout constraintLayout;
    private LinearLayout underline;  //下划线
    private boolean isShowArrow = true;
    private boolean isChange = false;

    private OnClickListener onClickListener;
    private OnCheckedChangeListener onCheckedChangeListener;
    private InverseBindingListener rightTextChangedListener;

    public CustomSettingItemView(Context context) {
        super(context);
        initView(context, null);
        initEvent();
    }

    public CustomSettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
        initEvent();
    }

    public CustomSettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
        initEvent();
    }

    /** 初始化视图 */
    private void initView(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.common_custom_setting_item, this, true);

        leftText = findViewById(R.id.common_left_text);
        rightText = findViewById(R.id.common_right_text);
        rightTextArrow = findViewById(R.id.common_right_text_arrow);
        aSwitch = findViewById(R.id.common_switch);
        constraintLayout = findViewById(R.id.common_constraintLayout);
        underline = findViewById(R.id.common_underline);

        rightTextArrow.setSelected(true);

        if (attrs != null){
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.common_CustomSettingItem);

            setLeftText(array.getString(R.styleable.common_CustomSettingItem_common_leftText));
            setRightText(array.getString(R.styleable.common_CustomSettingItem_common_rightText));
            isShowArrow = array.getBoolean(R.styleable.common_CustomSettingItem_common_isShowArrow, true);
            setShowRightTextArrow(isShowArrow);
            setEnable(array.getBoolean(R.styleable.common_CustomSettingItem_common_isEnable, true));
            setShowRightText(array.getBoolean(R.styleable.common_CustomSettingItem_common_isShowRightText, true));
            setShowSwitch(array.getBoolean(R.styleable.common_CustomSettingItem_common_isShowSwitch, false));
            setShowUnderline(array.getBoolean(R.styleable.common_CustomSettingItem_common_isShowUnderline, true));
            setLeftTextSize(array.getInt(R.styleable.common_CustomSettingItem_common_leftTextSize, 16));
            setIsShow(array.getBoolean(R.styleable.common_CustomSettingItem_common_isShow, true));
            float left = array.getDimension(R.styleable.common_CustomSettingItem_common_paddingLeft, 0);
            float right = array.getDimension(R.styleable.common_CustomSettingItem_common_paddingRight, 0);
            setPadding(left, right);
        }

    }

    private void setPadding(float left, float right){
        constraintLayout.setPadding((int) left, 0, (int) right, 0);
    }

    /** 初始化事件 */
    private void initEvent(){
        constraintLayout.setOnClickListener(v -> {
            if (onClickListener != null)
                onClickListener.onClick(v);
        });

        aSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (onCheckedChangeListener != null)
                onCheckedChangeListener.onCheckedChange(buttonView, isChecked);
        });

        aSwitch.setOnTouchListener((v, event) -> {
            isChange = true;
            return false;
        });
    }

    public boolean isChange() {
        return isChange;
    }

    public void setLeftText(String text) {
        leftText.setText(text);
    }

    public void setRightText(String text) {
        rightText.setText(text);
        rightTextArrow.setText(text);

        if (rightTextChangedListener == null) return;
        rightTextChangedListener.onChange();
    }

    public String getRightText(){
        return rightText.getText().toString();
    }

    public void setShowRightTextArrow(boolean showArrow) {
        rightTextArrow.setVisibility(showArrow ? VISIBLE : GONE);
        rightText.setVisibility(showArrow ? GONE : VISIBLE);
    }

    public void setEnable(boolean enable) {
        //constraintLayout.setBackgroundResource(enable ? R.color.white : R.color.common_setting_item_bg);
        constraintLayout.setClickable(enable);
        leftText.setTextColor(enable ? getResources().getColor(R.color.black) : getResources().getColor(R.color.text_gray_leve_3));
    }

    public void setShowRightText(boolean showRightText) {
        if (isShowArrow){
            rightTextArrow.setVisibility(showRightText ? VISIBLE : GONE);
        }else {
            rightText.setVisibility(showRightText ? VISIBLE : GONE);
        }
    }

    public void setShowSwitch(boolean showSwitch) {
        if (showSwitch){
            setShowRightText(false);
        }
        aSwitch.setVisibility(showSwitch ? VISIBLE : GONE);
    }

    public void setLeftTextSize(float size){
        leftText.setTextSize(size);
    }

    public void setShowUnderline(boolean showUnderline){
        underline.setVisibility(showUnderline ? VISIBLE : GONE);
    }

    public void setIsShow(boolean b){
        setVisibility(b ? VISIBLE : GONE);
    }

    public void setSwitchChecked(boolean b){
        aSwitch.setChecked(b);
    }

    public boolean getSwitchChecked(){
        return aSwitch.isChecked();
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    public interface OnClickListener{
        void onClick(View view);
    }

    public interface OnCheckedChangeListener{
        void onCheckedChange(CompoundButton buttonView, boolean isChecked);
    }

    /**
     * BindingAdapter
     */
    public static class MyBindingAdapter{

        @BindingAdapter(value = {"common_isEnable"})
        public static void setIsEnableItem(CustomSettingItemView view, boolean enable) {
            view.setEnable(enable);
        }

        @BindingAdapter(value = {"common_leftText"})
        public static void setLeftText(CustomSettingItemView view, String text) {
            view.setLeftText(text);
        }

        @BindingAdapter(value = {"common_isShowUnderline"})
        public static void setUnderline(CustomSettingItemView view, boolean enable){
            view.setShowUnderline(enable);
        }

        /** 双向绑定右边文字 */
        @BindingAdapter(value = {"common_rightText"})
        public static void setRightText(CustomSettingItemView view, String text) {
            if (null != text && !text.equals(view.getRightText())){
                view.setRightText(text);
            }
        }

        /** 双向绑定右边文字 */
        @InverseBindingAdapter(attribute = "common_rightText", event = "rightTextChange")
        public static String getRightText(CustomSettingItemView view){
            return view.getRightText();
        }

        /** 双向绑定右边文字 */
        @BindingAdapter(value = "rightTextChange")
        public static void rightTextChanged(CustomSettingItemView view, InverseBindingListener listener){
            view.rightTextChangedListener = listener;
        }

        @BindingAdapter(value = {"common_rightTextArrow"})
        public static void setRightTextArrow(CustomSettingItemView view, String text) {
            view.setRightText(text);
        }

        @BindingAdapter(value = {"common_isShow"})
        public static void setIsShow(CustomSettingItemView view, boolean b){
            view.setVisibility(b ? VISIBLE : GONE);
        }

        /** 双向绑定开关 */
        @BindingAdapter(value = {"common_isChecked"})
        public static void setChecked(CustomSettingItemView view, boolean b){
            if (!(view.aSwitch.isChecked() && b)){
                view.setSwitchChecked(b);
            }
        }

        /** 双向绑定开关 */
        @InverseBindingAdapter(attribute = "common_isChecked", event = "stateChange")
        public static boolean getChecked(CustomSettingItemView view){
            return view.aSwitch.isChecked();
        }

        /** 双向绑定开关 */
        @BindingAdapter(value = {"stateChange"})
        public static void switchStateChange(CustomSettingItemView view, InverseBindingListener listener){
            view.aSwitch.setOnClickListener(v -> {
                listener.onChange();
            });
        }
    }
}
